package MelbourneMap;

import java.awt.*;
import java.awt.geom.*;
import java.util.*;
 
/**
 * Region is an object containing point data about a region from
 * which it builds a {@link Shape} object.
 * <p>
 * A Region object is created by specifying a name, a list of 
 * {@link Node} objects, a type and the number of lanes. 
 * The list of nodes is immediately built into a shape representing
 * the region. 
 * 
 * @author David Zappia, 7183216
 * @author Heong Jin Yu, 7019777
 * 
 */
public class Region 
{
	public String name;
	protected ArrayList<Node> shapeData; //map of point data from file
	protected Shape shape; //Map of built shapes
	private int type;
	private Color color;
	public boolean selected;
	
	/**
	 * Class constructor specifying the name, data points to be used
	 * and the region type.
	 * 
	 * @param n			the name of the region
	 * @param fileData	the list of data points for the region
	 * @param rgType	the type of the region
	 */
	public Region(String n, ArrayList<Node> fileData, int rgType)
	{
		name = n;
		type = rgType;
		shapeData = fileData;
		shape = buildShape();
		selected = false;
		
		for (Node aNode : fileData)
			aNode.addRegion(this);
	}
	
	/**
	 * Creates a shape representing the region using the point 
	 * data provided. The Shape created is designed to be
	 * used when drawing a region.
	 * 
	 * @return	the Shape object for the region
	 */ 
	private Shape buildShape()
	{
		//boolean first = true;
		GeneralPath shape = new GeneralPath(); //new temp polygon shape

		shape.moveTo(shapeData.get(0).getX(), shapeData.get(0).getY());
			
		for (int i = 1; i < shapeData.size(); i++)
			shape.lineTo(shapeData.get(i).getX(), shapeData.get(i).getY());
		
		if (type == -1)
			shape.closePath(); // close the shape
			
		return shape;
	}
	
	/**
	 * Rebuilds the Shape object. To be used when then
	 * point data for the region has been altered and a 
	 * new shape needs to be created.
	 */
	public void rebuildShape()
	{
		shape = buildShape();
	}
	
	/**
	 * Gets the Shape object relating to this region.
	 * To be used when drawing the region.
	 * 
	 * @return		the Shape object relating to the region
	 */
	public Shape getShape() 
	{
		return shape;
	}
	
	/**
	 * Gets the type of a the region. To be used when the {@link Shape}
	 * object of the region is being drawn so that it can be drawn 
	 * with the correct colour and size.
	 * 
	 * @return		the integer value representing the region type
	 */
	public int getType()
	{
		return type;
	}	
	
	/**
	 * Gets the color to be used when drawing the region.
	 * 
	 * @return
	 */
	public Color getColor()
	{
		return color;
	}
	
	/**
	 * Sets the color to be used when the region is drawn.
	 * 
	 * @param pColor	the color to be used when drawing
	 */
	public void setColor(Color pColor)
	{
		color = pColor;
	}
	
	/**
	 * Gets the list object used to store the {@link Node} objects
	 * that form the region.
	 * 
	 * @return		the list of Nodes for the region
	 */
	public ArrayList<Node> getData()
	{
		return shapeData;
	}
	
	/**
	 * Prints all the data points for the region to the
	 * console.
	 */
	public void printData()
	{
		for (int i = 0; i < shapeData.size(); i ++)
		{
           String value = shapeData.get(i).toString();
           
           System.out.println(name);
           System.out.println(i + " " + value);  
        }
	}
	
	/**
	 * Adds two new nodes to the region so that
	 * it can be draw correctly.
	 * 
	 * @param newNode		a node to add
	 * @param newNode2		a second node to add
	 */
	public void addNode(Node newNode, Node newNode2)
	{
		if(!shapeData.contains(newNode))
			newNode.addRegion(this);
		if(!shapeData.contains(newNode2))
			newNode2.addRegion(this);
		
		shapeData.add(newNode);
		shapeData.add(newNode2);
		
		rebuildShape();
	}
	
	
}
